//
//  ViewController.m
//  Demo_NetowrkEncryptAndDecrypt
//
//  Created by apple on 2020/3/10.
//  Copyright © 2020 apple. All rights reserved.
//

#import "ViewController.h"
#import "NEDBuffer.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *username = @"TestUserName";
    NSString *password = @"TestPassword";
    
    NSString *URLString = @"http://www.blogshare.com/loginValdate.php";
    NSString *httpBody = [NSString stringWithFormat:@"username=%@&password=%@", username, password];
    
    NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfiguration];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URLString]];
    
    // 用前端和后台协议的加密方案
    NEDBuffer *buffer = [NEDBuffer defaultForAPI];
    [buffer setNSString:@"username"];
    [buffer setNSString:username];
    [buffer setNSString:@"password"];
    [buffer setNSString:httpBody];
    [buffer setLengthWithFirst];
    NSData *data = [NSData dataWithBytes:[buffer getBody] length:[buffer getLength]];
    request.HTTPBody = data;
    
//    request.HTTPBody = [httpBody dataUsingEncoding:NSUTF8StringEncoding];
    request.HTTPMethod = @"POST";
    
    NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"error: %@", [error localizedDescription]);
        } else {
            NSDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSLog(@"response data: %@", responseJSON);
        }
    }];
    [postDataTask resume];
}


@end
